package com.rytx.system.service.impl;

import java.util.List;
import com.rytx.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rytx.system.mapper.ShippingAddressesMapper;
import com.rytx.system.domain.ShippingAddresses;
import com.rytx.system.service.IShippingAddressesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收货地址Service业务层处理
 *
 * @author rytx
 * @date 2024-07-01
 */
@Service
public class ShippingAddressesServiceImpl implements IShippingAddressesService
{
    @Autowired
    private ShippingAddressesMapper shippingAddressesMapper;

    /**
     * 查询收货地址
     *
     * @param id 收货地址主键
     * @return 收货地址
     */
    @Override
    public ShippingAddresses selectShippingAddressesById(Long id)
    {
        return shippingAddressesMapper.selectShippingAddressesById(id);
    }
    @Override
    public ShippingAddresses selectShippingAddressesByIdAndUserId(ShippingAddresses shippingAddresses)
    {
        return shippingAddressesMapper.selectShippingAddressesByIdAndUserId(shippingAddresses);
    }

    /**
     * 查询收货地址列表
     *
     * @param shippingAddresses 收货地址
     * @return 收货地址
     */
    @Override
    public List<ShippingAddresses> selectShippingAddressesList(ShippingAddresses shippingAddresses)
    {
        return shippingAddressesMapper.selectShippingAddressesList(shippingAddresses);
    }

    /**
     * 新增收货地址
     *
     * @param shippingAddresses 收货地址
     * @return 结果
     */
    @Override
    public int insertShippingAddresses(ShippingAddresses shippingAddresses)
    {
        extracted(shippingAddresses);
        shippingAddresses.setCreateTime(DateUtils.getNowDate());
        return shippingAddressesMapper.insertShippingAddresses(shippingAddresses);
    }

    /**
     * 修改收货地址
     *
     * @param shippingAddresses 收货地址
     * @return 结果
     */
    @Override
    public int updateShippingAddresses(ShippingAddresses shippingAddresses)
    {
        extracted(shippingAddresses);
        shippingAddresses.setUpdateTime(DateUtils.getNowDate());
        return shippingAddressesMapper.updateShippingAddresses(shippingAddresses);
    }

    private void extracted(ShippingAddresses shippingAddresses) {
        if(shippingAddresses.getIsDefault() != null && shippingAddresses.getIsDefault().equals("1")){//如果有修改默认地址的先清空所有默认地址
            ShippingAddresses sa = new ShippingAddresses();
            sa.setUserId(shippingAddresses.getUserId());
            sa.setIsDefault("0");
            shippingAddressesMapper.updateShippingAddressesByUserId(sa);
        }
    }

    /**
     * 批量删除收货地址
     *
     * @param ids 需要删除的收货地址主键
     * @return 结果
     */
    @Override
    public int deleteShippingAddressesByIds(Long[] ids)
    {
        return shippingAddressesMapper.deleteShippingAddressesByIds(ids);
    }

    /**
     * 删除收货地址信息
     *
     * @param id 收货地址主键
     * @return 结果
     */
    @Override
    public int deleteShippingAddressesById(Long id)
    {
        return shippingAddressesMapper.deleteShippingAddressesById(id);
    }
    /**
     * 清除当前用户所欲收货地址默认状态
     */
    @Override
    public void cleardefaultaddress(Long userId) {
        shippingAddressesMapper.cleardefaultaddress(userId);
    }
    /**
     * 修改收货地址默认状态
     */
    @Transactional
    @Override
    public void updatedefaultaddress(Long userId,Long id) {
        shippingAddressesMapper.cleardefaultaddress(userId);
        shippingAddressesMapper.updatedefaultaddress(userId,id);
    }
    /**
     * 获取用户默认收货地址
     */
    @Override
    public ShippingAddresses getdefaultaddress(Long userId) {
      return  shippingAddressesMapper.getdefaultaddress(userId);
    }
}
